package com.sms.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class ReceiveMsgServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public ReceiveMsgServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
		}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		
	    String phone = request.getParameter("dest");
	    String[] phoneList = phone.split(";");
	    String rep = "";
	    for(int j=0;j< phoneList.length;j++){
			int ra =(new Random()).nextInt(1000000);
			String errRepCode[] = {"100","101","104","106","107","108","111"};
			String errRepMsg[] = {"余额不足","账号关闭","用户访问时间间隔低于50毫秒","用户名不存在","密码错误","指定访问ip错误","短信内容内有敏感词"};
			String resString = "";
			if(ra<70000){
				resString = errRepCode[ra%7];
			}else{
				SimpleDateFormat sd = new SimpleDateFormat("yyyyMMddHHmmss");
				resString = sd.format(new Date()) + ra;
			}
	        if (j != phoneList.length-1){
	            rep = rep + resString+";";
	        }else{
	            rep = rep + resString;
	        }
	    }
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out = response.getWriter();
		out.println(rep);
		out.flush();
		out.close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
